//---------------------------------------------------------------------------


#pragma hdrstop

#include "TControlStateStatistic.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

__fastcall TControlStateStatistic::TControlStateStatistic()
{
	Result.Boost.clear();
	Result.CtrRetarder.clear();
}

TUnitTimeQuantity TUnitTimeQuantity::operator + (TUnitTimeQuantity t)
{
	TUnitTimeQuantity Result;
	int i;
	Result.IntervalTime = t.IntervalTime;
	Result.EndTime = t.EndTime;
	Result.Boost.resize(t.Boost.size());
	Result.CtrRetarder.resize(t.CtrRetarder.size());
	for (i = 0; i < t.CtrRetarder.size(); i++) {
		Result.Boost[i] = Boost[i] + t.Boost[i];
		Result.CtrRetarder[i] = CtrRetarder[i] + t.CtrRetarder[i];
	}
	return Result;
}

//---------------------------------------------------------------------------
void __fastcall TControlStateStatistic::AddItem(const int CutNumber, const float Time,
																const bool CtrState,
																const int CtrRetarderQuantity)
{
	TControlStateItem Item;
	Item.CutNumber = CutNumber;
	Item.Time = Time;
	Item.State = CtrState;
	Item.CtrRetarderQuantity = CtrRetarderQuantity;
	push_back(Item);
}

//---------------------------------------------------------------------------
TUnitTimeQuantity __fastcall TControlStateStatistic::GetUnitTimeQuantity(const unsigned short Index,
																								 const float EndTime,
																								 const float UnitTime)
{
	int i, q, j, k;
	float t;
	Result.Boost.clear();
	Result.CtrRetarder.clear();
	Result.IntervalTime = UnitTime;
//	Result.CtrRetarder.IntervalTime = UnitTime;
	if (EndTime == 0 || UnitTime < 0.001)
		return Result;
	Result.Boost.resize(EndTime / UnitTime + 1);
	Result.CtrRetarder.resize(EndTime / UnitTime + 1);
	switch (int(Index / 1000)) {
		case 4:
			for (i = 0; i < size(); i++) {
				if (int(at(i).Time / UnitTime) > Result.Boost.size()) continue;
				Result.Boost[int(at(i).Time / UnitTime)] += at(i).CtrRetarderQuantity;
			}
			break;
		case 1:
		case 2:
			for (i = 0; i < size(); i++) {
				if (int(at(i).Time / UnitTime) > Result.CtrRetarder.size()) continue;
				if (Result.CtrRetarder[int(at(i).Time / UnitTime)] > 0)
					continue;
				else
					Result.CtrRetarder[int(at(i).Time / UnitTime)] = at(i).CtrRetarderQuantity;
			}
			break;
		default:
		;
	}
	return Result;
}


